package org.atovio.mybatis.mapper;

import org.apache.ibatis.annotations.*;
import org.atovio.mybatis.pojo.ArticlePO;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author AtoVio, Winter Yuan
 */
public interface ArticleMapper {

    // 按主键查询
    @Select("""
            select id, user_id, title, create_time, update_time
            from article
            where id = #{id}
            """)
    // 使用结果集Results 和 对象 Object的 映射
    // 开启驼峰命名后，大部分情况下不需要配置
    @Results(id = "BaseArticleMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "title", property = "title"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
    })
    ArticlePO selectById(@Param("id") Integer id);

    @Select("select * from article")
    // 可以使用@ResultMap注解调用写好的结果映射
    @ResultMap("BaseArticleMap")
    List<ArticlePO> selectAll();

    // insert
    @Insert("""
            insert into article(user_id, title, create_time, update_time)
            values (#{userId}, #{title}, #{createTime}, #{updateTime})
            """)
    int insertArticle(ArticlePO articlePO);

    // update
    @Update("update article set update_time = #{updateTime} where id = #{id}")
    int updateUpdateTimeById(Integer id, LocalDateTime updateTime);

    // delete
    @Delete("delete from article where id = #{id}")
    int deleteById(Integer id);
}
